//
//  MODepthDoDetail_Text_OtherViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/26.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MODepthDoDetail_Text_OtherViewController.h"
#import "MOWaitDoDetail_MainViewCell.h"
#import "MOHTMLLoadViewController.h"


@interface MODepthDoDetail_Text_OtherViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
//数据数组
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MODepthDoDetail_Text_OtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self initData];
}

-(void)setWaitAgainModel:(MONoticeModel *)waitAgainModel
{
    _waitModel = waitAgainModel;
    [self initData];
}


-(void)initData
{
    self.dataArray = [NSMutableArray array];
    
    if (self.waitModel.fileList) {
        NSArray *titleArr = self.waitModel.fileList;
        NSMutableArray *muArr = [NSMutableArray array];
        for (int i = 0; i<titleArr.count; i++) {
            
            MOWaitDoDetail_MainModel *model = [[MOWaitDoDetail_MainModel alloc]init];
            model.fileTitle = titleArr[i][@"fileName"];
            model.alias = titleArr[i][@"fileTitle"];
            model.domain = titleArr[i][@"domain"];
            model.path = titleArr[i][@"filePath"];
            
            [muArr addObject:model];
            
        }
        [self.dataArray addObject:@{@"arr":muArr,@"isOpen":@"1"}];
    }else{
        [self.dataArray addObject:@{@"arr":[NSMutableArray array],@"isOpen":@"1"}];
    }
    
    [self.tableView reloadData];
    
}

-(void)clickRightButton:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    if (sender.selected==YES) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[0]];
        [dic setObject:@"1" forKey:@"isOpen"];
        [self.dataArray replaceObjectAtIndex:0 withObject:dic];
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[0]];
        [dic setObject:@"0" forKey:@"isOpen"];
        [self.dataArray replaceObjectAtIndex:0 withObject:dic];
    }
    [self.tableView reloadData];
    
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray *arr = self.dataArray[0][@"arr"];
    if ([self.dataArray[0][@"isOpen"] integerValue]==1) {
        return arr.count;
    }else{
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_MainViewCell *cell = [MOWaitDoDetail_MainViewCell cellWithTableView:tableView andIndexPath:indexPath];
    NSArray *arr = self.dataArray[0][@"arr"];
    MOWaitDoDetail_MainModel *model = arr[indexPath.row];
    cell.model = model;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.dataArray[0][@"arr"];
    MOWaitDoDetail_MainModel *model = arr[indexPath.row];
    return [MOWaitDoDetail_MainViewCell heightForMOWaitDoDetail_MainViewCellAndModel:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHConversions(54)+10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54)+10)];
    view.backgroundColor = KGrayColor_ededed;
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(54))];
    backView.backgroundColor = [UIColor whiteColor];
    [view addSubview:backView];
    
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(17, 0, 100, kHConversions(54))];
    
    [leftButton setTitle:@" 附件" forState:UIControlStateNormal];
    [leftButton setImage:IMAGE(@"fujian") forState:UIControlStateNormal];
    [leftButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
    [leftButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    leftButton.titleLabel.font = FONT_SIZE(15);
    
    [backView addSubview:leftButton];
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-17-50, 0, 50, kHConversions(54))];
    if ([self.dataArray[0][@"isOpen"] integerValue]==1) {
        rightButton.selected = YES;
    }else{
        rightButton.selected = NO;
    }
    
    [rightButton setImage:IMAGE(@"top_notice") forState:UIControlStateSelected];
    
    [rightButton setImage:IMAGE(@"right_notice") forState:UIControlStateNormal];
    [rightButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    
    rightButton.tag = section;
    [rightButton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [backView addSubview:rightButton];
    
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWaitDoDetail_MainModel *model = self.dataArray[0][@"arr"][indexPath.row];
    
    MOHTMLLoadViewController *vc = [[MOHTMLLoadViewController alloc]init];
    vc.fileName = model.fileTitle;
    vc.key = self.waitModel.key;
    vc.comeType = @"4";
    vc.filePath = model.path;
    vc.domain = model.domain;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - 41) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
